package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.util.CurrentUserContextHolders;
import com.qk.domain.business.BusinessListDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessFollowDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolListDTO;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import com.qk.vo.businesstrackrecord.BusinessTrackRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * &#064;author：zzq
 * &#064;createTime：2025/10/15 10:54
 * &#064;description： 
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessTrackRecordMapper businesstrackrecordmapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<BusinessListDO> businessList(BusinessListDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<BusinessListDO> page = (Page<BusinessListDO>) this.baseMapper.selectBusinessList(dto);
        return PageResult.<BusinessListDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

    @Override
    public void addBusiness(BusinessDTO dto) {
        Business business = BeanUtil.copyProperties(dto, Business.class);
        //补全数据
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(business);
    }

    //根据id查询商机详情

    @Override
    public BusinessVO getBusinessInfoById(Integer id) {
        //1.根据id查询商机表获取基础线索信息
        Business business = this.baseMapper.selectById(id);
        //2.根据商机id获取对应商机跟进记录
        List<BusinessTrackRecordVO> trackRecords = this.baseMapper.selectTrackRecordByBusinessId(id);
        //3.封装数据并返回
        BusinessVO vo = BeanUtil.copyProperties(business, BusinessVO.class);
        vo.setTrackRecords(trackRecords);
        return vo;
    }

    @Override
    public void distributeBusiness(Integer businessId, Integer userId) {
        Business business = this.baseMapper.selectById(businessId);
        business.setUserId(userId);
        business.setStatus(BusinessStatusConstants.WAIT_FOLLOW_UP);
        this.baseMapper.updateById(business);
    }

    @Override
    @Transactional
    public void followUpBusiness(BusinessFollowDTO dto) {
        //根据id查询商机
        Business business = this.baseMapper.selectById(dto.getId());
        //封装数据
        BeanUtil.copyProperties(dto, business);
        //修改状态
        if (dto.getStatus() == BusinessStatusConstants.WAIT_FOLLOW_UP) {
            business.setStatus(BusinessStatusConstants.FOLLOW_UP);
        }
        //修改时间
        business.setUpdateTime(LocalDateTime.now());
        //修改
        this.baseMapper.updateById(business);
        //添加跟进记录
        BusinessTrackRecord businessTrackRecord = BeanUtil.copyProperties(dto, BusinessTrackRecord.class);
        businessTrackRecord.setId(null);
        businessTrackRecord.setUserId(CurrentUserContextHolders.get());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecord.setBusinessId(dto.getId());
        businesstrackrecordmapper.insert(businessTrackRecord);
    }

    @Override
    public void backToPool(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setStatus(BusinessStatusConstants.FAKE_BUSINESS);
        this.baseMapper.updateById(business);
    }

    @Override
    @Transactional
    public void toCustomer(Integer id) {
        //修改商机状态为转客户
        Business business = this.baseMapper.selectById(id);
        business.setStatus(BusinessStatusConstants.CONVERT_TO_CUSTOMER);
        try {
            this.baseMapper.updateById(business);
        } catch (Exception e) {
            log.error("修改商机状态为转客户失败", e);
        }
        //新增信息至客户表
        Customer customer = BeanUtil.copyProperties(business, Customer.class);
        //补全信息
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        try {
            customerMapper.insert(customer);
        } catch (Exception e) {
            log.error("新增信息至客户表失败", e);
        }
    }

    @Override
    public PageResult<Business> businessPoolList(BusinessPoolListDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<Business> page = (Page<Business>) this.baseMapper.selectBusinessPoolList(dto);
        return PageResult.<Business>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }
}
